<?php

namespace app\admin\controller;

use app\service\RecordService;
use app\service\roominfoService;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use think\db\Where;
use think\facade\Db;
use think\facade\View;

class Record extends Common
{
    //业主家人开门记录
    public function index(){
        //查询小区信息
        $com = $this->pidTokenId();

        $whe[] = ['community_id','in',$com['community_id']];//所有小区ID
        $communitylist = (new roominfoService())->communityList($whe);

        $community_id = $com['com_id'];
        View::assign('communitylist',$communitylist);


        //查询
        $data = request()->get();
        //$where[] = ['community_id','in',$com['community_id']];
        $where=[];
        if (!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }

            if(!empty($data['mobile'])){
                $where['mobile'] = $data['mobile'];
            }
            if(!empty($data['owner_name'])){
                $where[] = ["name","like","%".$data['owner_name']."%"];
            }
            if(!empty($data['device_name'])){
                $where[] = ["device_name","like","%".$data['device_name']."%"];
            }

            if(!empty($data['device_type'])){
                $where['device_type'] = $data['device_type'];
            }
            if((!empty($data['start_time']))&&(empty($data['end_time']))){
                $where[]=['create_time','>=',$data['start_time']];
            }
            if((!empty($data['end_time']))&&(empty($data['start_time']))){
                $where[]=['create_time','<=',$data['end_time']];
            }
            if((!empty($data['start_time']))&&(!empty($data['end_time']))){
                $where[]=[ 'create_time',[">=",$data['start_time']],["<=",$data['end_time']],'and'];
            }
            View::assign('where',$where);
            $where = array_filter($where);
        }
        //var_dump($where);
        $export = $where;
        $export = json_encode($export);
        $export = str_replace("\"","|",$export);
        View::assign('export',$export);
        $page = request()->has('page') ? request()->get('page') : 1;
        //查询房屋条数

        $number =(new recordService())->recordCount($community_id,$where);
        //var_dump($number);
        View::assign('number',$number);
        //查询房屋信息

        $dataList = (new recordService())->getRecordList($community_id,$page,$where);
        //var_dump($roomlist);

        //判断是否有数据
        if(!empty($number)){
            foreach($dataList as $room){


                $list[] = $room;
                View::assign('list',$list);
            }
        }else{
            View::assign('list',$dataList);
        }
        //View::assign('list',$roomlist);
        View::assign('dataList',$dataList);

        foreach ($data as $k => $v) {
            View::assign($k, $v);
        }


        return View::fetch('index');
    }
    //骑手开门记录
    public function rider(){

    }

    public function stranger(){
        //查询小区信息
        $com = $this->pidTokenId();

        //$whe['community_id'] = array('in',$com['community_id']);//所有小区ID
        $whe[] = ['community_id','in',$com['community_id']];//所有小区ID
        $communitylist = (new roominfoService())->communityList($whe);

        $community_id = $com['com_id'];
        View::assign('communitylist',$communitylist);


        //查询
        $data = request()->get();
        $where=[];
        //$where[] = ['community_id','in',$com['com_id']];
        if (!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }else{
                $where['community_id'] = "";
            }

            if(!empty($data['mobile'])){
                $where['mobile'] = $data['mobile'];
            }
            if(!empty($data['owner_name'])){
                $where[] = ["name","like","%".$data['owner_name']."%"];
            }
            if(!empty($data['device_name'])){
                $where[] = ["device_name","like","%".$data['device_name']."%"];
            }
            if(!empty($data['device_type'])){
                $where['device_type'] = $data['device_type'];
            }
            if((!empty($data['start_time']))&&(empty($data['end_time']))){
                $where[]=['create_time','>=',$data['start_time']];
            }
            if((!empty($data['end_time']))&&(empty($data['start_time']))){
                $where[]=['create_time','<=',$data['end_time']];
            }
            if((!empty($data['start_time']))&&(!empty($data['end_time']))){
                $where[]=[ 'create_time',[">=",$data['start_time']],["<=",$data['end_time']],'and'];
            }
            View::assign('where',$where);
            $where = array_filter($where);
        }
        $export = $where;
        $export = json_encode($export);
        $export = str_replace("\"","|",$export);
        View::assign('export',$export);
        $page = request()->has('page') ? request()->get('page') : 1;
        //查询房屋条数

        $number =(new recordService())->strangerCount($community_id,$where);
        //var_dump($number);
        View::assign('number',$number);
        //查询房屋信息

        $dataList = (new recordService())->getStrangerList($community_id,$page,$where);
        //var_dump($roomlist);

        //判断是否有数据
        if(!empty($number)){
            foreach($dataList as $room){


                $list[] = $room;
                View::assign('list',$list);
            }
        }else{
            View::assign('list',$dataList);
        }
        //View::assign('list',$roomlist);
        View::assign('dataList',$dataList);

        foreach ($data as $k => $v) {
            View::assign($k, $v);
        }


        return View::fetch('stranger');

    }

    public function strangerExport(){

        //获取账单数据
        $export = request()->param('export');
        $export = str_replace("|","\"",$export);

        $where = json_decode($export,true);

        $com = $this->pidTokenId();
        $community_id = $com['com_id'];
        $recordList=Db::name('records_stranger')
            ->where('community_id','in',$community_id)
            ->where($where)
            ->order('id desc')
            ->select();

        if(empty($recordList)){
            return "没有数据";
        }

        $newExcel = new Spreadsheet();  //创建一个新的excel文档
        $objSheet = $newExcel->getActiveSheet();  //获取当前操作sheet的对象
        //设置宽度为true,不然太窄了
        $newExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
        $newExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        //设置第一栏的标题

        $objSheet->setTitle('开门记录');  //设置当前sheet的标题
        //设置第一栏的标题
        $objSheet->setCellValue('A1', '小区')
            ->setCellValue('B1', '门锁名称')
            ->setCellValue('C1', '手机号码')
            ->setCellValue('D1', '姓名')
            ->setCellValue('E1', '时间');



        $i=2;  //定义一个i变量，目的是在循环输出数据是控制行数
        $count =Db::name('records_stranger')
            ->where('community_id','in',$community_id)
            ->where($where)->count();  //计算有多少条数据
        for ($i = 2; $i <= $count+1; $i++) {
            $objSheet->setCellValue('A' . $i, $recordList[$i-2]["community_name"]);
            $objSheet->setCellValue('B' . $i,$recordList[$i-2]["device_name"]);
            $objSheet->setCellValue('C' . $i, $recordList[$i-2]["mobile"]);
            $objSheet->setCellValue('D' . $i, $recordList[$i-2]["name"]);
            $objSheet->setCellValue('E' . $i, $recordList[$i-2]["create_time"]);
//            $paytype="支付宝扫码";
//            if($recordList[$i-2]["scenes"]=="2"){
//                $paytype="支付宝预支付";
//            }
//            if($recordList[$i-2]["scenes"]=="3"){
//                $paytype="微信扫码";
//            }
//            $objSheet->setCellValue('E' . $i,$paytype);
//            $objSheet->setCellValue('F' . $i, $orderlist[$i-2]["createTime"]);
//            $objSheet->setCellValue('G' . $i, $orderlist[$i-2]["inTime"]);
//            $objSheet->setCellValue('H' . $i, $orderlist[$i-2]["outTime"]);
//            $objSheet->setCellValue('I' . $i, convertTime($orderlist[$i-2]["stopTime"]));
//            $objSheet->setCellValue('J' . $i, $orderlist[$i-2]["out_trade_no"]);
//            $objSheet->setCellValue('K' . $i, $orderlist[$i-2]["trade_no"]);

        }

        $file_name = '开门记录.xlsx';
        // 实例化导出类
        header('Content-Type:application/vnd.ms-excel');
        header('Content-Disposition:attachment;filename=' . $file_name);
        header('Cache-Control:max-age=0');

        $writer = IOFactory::createWriter($newExcel, 'Xlsx');
        $writer->save('php://output');

//        $writer = new Xlsx($objSheet);
//        $writer->save('hello_world.xlsx');

        //$this->downloadExcel($newExcel, "停车账单", 'Xls');
    }

}